package com.sk.bilibili.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.event.BiliBiliEvent;
import com.sk.bilibili.other.R;
import com.sk.bilibili.utils.BSessionUtils;
import com.sk.bilibili.utils.QueryWrapperUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import com.sk.bilibili.bean.CoinRecord;
import com.sk.bilibili.mapper.CoinRecordMapper;

import java.util.List;

/**
* @author 邵康
* @date 2021/12/04 18:54
*/
@Service
public class CoinRecordService extends ServiceImpl<CoinRecordMapper,CoinRecord> {

    public R<Void> add(CoinRecord coinRecord) {
        save(coinRecord);
        return R.success();
    }

    public R<Void> delete(Integer id) {
        removeById(id);
        return R.success();
    }

    public R<Void> update(CoinRecord coinRecord) {
        updateById(coinRecord);
        return R.success();
    }

    @EventListener(condition = "event.type == 'com.sk.bilibili.service.MemberService.changeCoin'")
    public void handleCoinAdd(BiliBiliEvent<CoinRecord> event){
        CoinRecord coinRecord = event.getData();
        save(coinRecord);//直接保存 即可
    }

    public R<List<CoinRecord>> getAll() {
        QueryWrapper<CoinRecord> queryWrapper = QueryWrapperUtils.eq("member_id", BSessionUtils.getMemberId());
        return R.success(list(queryWrapper));
    }

}
